package com.ctsi.commons.conversion;

import java.io.IOException;
import java.net.*;

/** Net Converter classes. */
public class NetConverters implements ConverterLoader {


    public static class StringToInetAddress extends AbstractConverter<String, InetAddress> {
        public StringToInetAddress() {
            super(String.class, InetAddress.class);
        }
        @Override
        public InetAddress convert(String obj) throws ConversionException {
            try {
                return InetAddress.getByName(obj);
            } catch (IOException e) {
                throw new ConversionException(e.getMessage(), e);
            }
        }
    }

    public static class InetAddressToString extends AbstractConverter<InetAddress, String> {
        public InetAddressToString() {
            super(InetAddress.class, String.class);
        }
        @Override
        public String convert(InetAddress obj) throws ConversionException {
            String hostName = obj.getHostName();
            if (hostName != null) return hostName;
            return obj.getHostAddress();
        }
    }

    public static class StringToURI extends AbstractConverter<String, URI> {
        public StringToURI() {
            super(String.class, URI.class);
        }
        @Override
        public URI convert(String obj) throws ConversionException {
            try {
                return new URI(obj);
            } catch (URISyntaxException e) {
                throw new ConversionException(e.getMessage(), e);
            }
        }
    }

    public static class URIToString extends AbstractConverter<URI, String> {
        public URIToString() {
            super(URI.class, String.class);
        }
        @Override
        public String convert(URI obj) throws ConversionException {
            return obj.toString();
        }
    }

    public static class StringToURL extends AbstractConverter<String, URL> {
        public StringToURL() {
            super(String.class, URL.class);
        }
        @Override
        public URL convert(String obj) throws ConversionException {
            try {
                return new URL(obj);
            } catch (MalformedURLException e) {
                throw new ConversionException(e.getMessage(), e);
            }
        }
    }

    public static class URLToString extends AbstractConverter<URL, String> {
        public URLToString() {
            super(URL.class, String.class);
        }
        @Override
        public String convert(URL obj) throws ConversionException {
            return obj.toString();
        }
    }

    public static class URIToURL extends AbstractConverter<URI, URL> {
        public URIToURL() {
            super(URI.class, URL.class);
        }
        @Override
        public URL convert(URI obj) throws ConversionException {
            try {
                return obj.toURL();
            } catch (MalformedURLException e) {
                throw new ConversionException(e.getMessage(), e);
            }
        }
    }

    public static class URLToURI extends AbstractConverter<URL, URI> {
        public URLToURI() {
            super(URL.class, URI.class);
        }
        @Override
        public URI convert(URL obj) throws ConversionException {
            try {
                return obj.toURI();
            } catch (URISyntaxException e) {
                throw new ConversionException(e.getMessage(), e);
            }
        }
    }
    @Override
    public void loadConverters() {
        Converters.loadContainedConverters(NetConverters.class);
    }
}
